package yxy.game.pm2.active.command.warlog;


import yxy.apple.logger.Logger;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_征战日志;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.subordinateclass.zztask;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActWarLogTaskDrawRewardRequest;

import java.util.HashMap;

@MessageType(proto = Command.ActWarLogTaskDrawReward, classtype = ActiveMessage.ActWarLogTaskDrawRewardRequest.class)
public class CMD_Active_征战日志_DrawTaskReward extends Command<ActWarLogTaskDrawRewardRequest> {

	private static final Logger logger = Logger.create(CMD_Active_征战日志_DrawTaskReward.class);
	
	@Override
	public void execute(MessageSource messageSource, ActWarLogTaskDrawRewardRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActWarLogTaskDrawRewardResponse.Builder response = ActiveMessage.ActWarLogTaskDrawRewardResponse.newBuilder();
		
		int taskid = request.getId();
		
		ActiveModule_征战日志 am = ActiveModule_征战日志.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(am.getRunday() < 7) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		zztask zztask = null;
		for(String taskcode : am.getRunningcodetaskmap().keySet()) {
			yxy.game.pm2.active.module.subordinateclass.zztask codezztask = am.getRunningcodetaskmap().get(taskcode);
			if(codezztask.getTaskid() == taskid) {
				zztask = codezztask;
			}	
		}
		
		if(zztask == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		Task task = Task.get(playercode, zztask.getTaskcode());
		if(task.isReward()) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		if(!task.isFinish()) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		
		task.setReward(true);
		task.set();
		
		int zzexp = am.getZzexp();
		zzexp = zzexp + zztask.getAddzzexp();
		am.setZzexp(zzexp);
		am.set();
		
		response.setAddzzexp(zztask.getAddzzexp());
		response.setZzexp(am.getZzexp());
		
		HashMap<String, yxy.game.pm2.active.module.subordinateclass.zztask> zzruntaskmap = am.getRunningcodetaskmap();
		for (String zztaskcode : zzruntaskmap.keySet()) {
			yxy.game.pm2.active.module.subordinateclass.zztask zztask2 = zzruntaskmap.get(zztaskcode);
			ActiveMessage.WarLogTaskInfo.Builder taskinfo = ActiveMessage.WarLogTaskInfo.newBuilder();
			taskinfo.setCode(zztask2.getTaskcode());
			taskinfo.setTaskid(zztask2.getTaskid());
			
			Task runtask = Task.get(playercode, zztaskcode);
			int canget = runtask.isReward() ? DrawStatus.hadget.getStatus() : runtask.isFinish() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			taskinfo.setProgress(runtask.getProgress());
			taskinfo.setCanget(canget);
			
			response.addTaskinfo(taskinfo.build());
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActWarLogTaskDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
}
